package com.qf.controller;

import com.qf.pojo.Result;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LoginController {
    @RequestMapping("/doLogin")
    public String doLogin(String username,String password,boolean rememberMe){
        password = new Md5Hash(password, username, 1024).toString();
        System.out.println("=====password======="+password);
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        if (rememberMe){
            token.setRememberMe(true);
        }
        Subject subject = SecurityUtils.getSubject();

        try {
            subject.login(token);
        }  catch (UnknownAccountException e) {
            return "/login.jsp";

        }catch (IncorrectCredentialsException e) {
            return "/login.jsp";

        } catch (AuthenticationException e) {
            return "/login.jsp";
        }
        System.out.println("++++++++++++++++++++++++++++++");
        return "/index.jsp";
    }
    @RequestMapping("/logout")
    @ResponseBody
    public Result logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return Result.ok("退出成功!");
    }
}
